
import { routerTableNames, routerDetailNames, routerEditNames } from '@/utils/mobileConfig';// 适配页面路由
import { isMobile } from '@/utils/tools';

const app = {
  state: {
    sidebar: {
      opened: !!+(localStorage.getItem('sidebarStatus') || 1),
      withoutAnimation: false
    },
    message: '',
    device: 'desktop',
    isMobile: false,
    noticeNum: 0,
    imgPreviewObj: {
      showImageViewer: false,
      imgData: [],
      selectIndex: 0,
      appendToBody: false
    },
    waitApproverNum: 0, // 待我处理的审批数量
    showMobilePage: false,
    mobilePage: [...routerDetailNames, ...routerTableNames, ...routerEditNames] // 这里面的页代表会进行移动端适配，填路由name
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      state.sidebar.opened = !state.sidebar.opened;
      if (state.sidebar.opened) {
        localStorage.setItem('sidebarStatus', '1');
      } else {
        localStorage.setItem('sidebarStatus', '0');
      }
      state.sidebar.withoutAnimation = false;
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      localStorage.setItem('sidebarStatus', '0');
      state.sidebar.opened = false;
      state.sidebar.withoutAnimation = withoutAnimation;
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device;
      state.isMobile = device === 'mobile';
    },
    TOGGLE_SHOW_TYPE: (state, showMobilePage) => {
      state.showMobilePage = showMobilePage && isMobile();
    },
    SETMSG: (state, msg) => {
      state.message = msg;
    },
    SETNOTICENUM: (state, { total, unTotal }) => {
      state.noticeNum = total + unTotal;
      state.waitApproverNum = total;
    },
    TOGGLE_IMAGE_VIEWER: (state, params) => {
      state.imgPreviewObj = { ...state.imgPreviewObj, ...params };
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR');
    },
    CloseSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation);
    },
    ToggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device);
    },
    ToggleShowType({ commit }, showMobilePage) {
      commit('TOGGLE_SHOW_TYPE', showMobilePage);
    },
    SetMsg({ commit }, msg) {
      commit('SETMSG', msg);
    },
    ToggleImageViewer: ({ commit }, params) => {
      commit('TOGGLE_IMAGE_VIEWER', params);
    }
  }
};

export default app;
